/**
 * 
 */
package neptune.web.client.services.experiment;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.PopupPanel;

import neptune.web.client.WebInterface;
import neptune.web.client.view.ConsoleManager;
import neptune.web.client.view.components.MessageWidget;
import neptune.web.client.view.components.NodesManager;
import neptune.web.client.view.components.NodesManager.NodeLiveDataProvider;

/**
 * Fornisce le informazioni riguardanti i nodi vistuali definiti in seno ad un
 * dato esperimento.
 * 
 * @author Roberto Bifulco [RobertoBifulco.it] [info@robertoBifulco.it]
 * 
 */
public class ExperimentNodesDataProvider implements NodeLiveDataProvider {

	private final String expId;

	public ExperimentNodesDataProvider(String expId) {
		this.expId = expId;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.client.view.components.NodesManager.NodeLiveInfosProvider
	 * #getNodeInfos(java.lang.String)
	 */
	public void getNodeInfos(final String nodeId, final NodesManager manager) {
		String message = "Retrieving node '" + nodeId + "." + expId
				+ "' infos...";

		ConsoleManager.addConsoleMessage(message, false);
		manager.setVMIcon(nodeId, NodesManager.UPDATING_ICON);

		ExperimentService.Util.getInstance().getNodeState(expId, nodeId,
				new AsyncCallback<String>() {

					public void onSuccess(String result) {
						ConsoleManager.addConsoleMessage("Node '" + nodeId
								+ "." + expId + "' infos retrieved", false);
						manager.setVMIcon(nodeId, NodesManager.RUNNING_ICON);
						manager.setNodeInfos(nodeId, result);
					}

					public void onFailure(Throwable caught) {
						ConsoleManager.addConsoleMessage("Unable to retrieve '"
								+ nodeId + "." + expId + "' infos. Cause:\n "
								+ caught.getMessage(), true);
						manager
								.setVMIcon(nodeId,
										NodesManager.NOT_IN_SYNC_ICON);
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * neptune.web.client.view.components.NodesManager.NodeLiveInfosProvider
	 * #getNodesInfos()
	 */
	public void getNodesInfos(final NodesManager manager) {
		String message = "Retrieving nodes infos for experiment '" + expId
				+ "'...";

		final PopupPanel popup = WebInterface.showMessageDialog(
				new MessageWidget(MessageWidget.LOADING, message), false);
		ConsoleManager.addConsoleMessage(message, false);
		
		
		ExperimentService.Util.getInstance().getAllocatedNodes(expId,
				new AsyncCallback<String[][]>() {

					public void onSuccess(String[][] result) {
						popup.hide();
						ConsoleManager.addConsoleMessage(
								"Node infos for experiment '" + expId
										+ "' retrieved", false);
						for (String[] data : result) {
							manager.addNode(data[0], data[1], data[2]);
						}
					}

					public void onFailure(Throwable caught) {
						popup.hide();

						WebInterface.addMessageToConsoleAndShowDialog(
								"Unable to retrieve node infos for experiment '"
										+ expId + "'. Cause: \n"
										+ caught.getMessage(), true);
					}
				});
	}

}
